<template>
  <el-col :span="6">
    <el-form-item :label="labelName" class="cust-label" style="text-align: left;">
      <el-tag
        :key="tag"
        v-for="tag in dynamicTags"
        closable
        :disable-transitions="false"
        @close="handleClose(tag)"
      >{{tag}}</el-tag>

      <el-input
        class="input-new-tag"
        v-if="inputVisible"
        v-model="inputValue"
        ref="saveTagInput"
        size="small"
        @keyup.enter.native="handleInputConfirm"
        @blur="handleInputConfirm"
      ></el-input>
      <el-button v-else class="button-new-tag" size="mini" @click="showInput">+ 添加标签</el-button>
    </el-form-item>
  </el-col>
</template>
<script>
export default {
  props: {
    // 接收页面初始化数据
    getVal: { type: String, default: "" },
    codeField: { type: String, default: "" },
    lookuptype: { type: String, default: "DB0043" },
    labelName:{type:String,default:"客户标签"},
  },
  data() {
    return {
      dynamicTags: [],
      inputValue: "",
      inputVisible: false
    };
  },
  //监听标签值
  watch: {
    dynamicTags(value) {
      let val = value.join(",");
      var parent = this.$utils.getHasFormFieldParentComponent(this);
      parent.formField[this.codeField] = val;
      this.$emit("changeCode", value);
    },
    getVal(xzz) {
      let val = [];
      this.dynamicTags = xzz.split(",");
    }
  },
  // watch:{

  //   getVal(xzz) {
  //     let val=[];
  //       val = xzz.split(",");
  //     return val;
  //   }
  // },
  mounted() {
    this.$nextTick(() => {
      this.getData();
    });
  },
  methods: {
    // 点击选择标签
    handleClose(tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
    },
    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.dynamicTags.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = "";
    },
    // 初始化
    getData() {
      let that = this;
      var parent = this.$utils.getHasFormFieldParentComponent(this);
      let val = [];
      if (parent.formField[this.codeField] != "") {
        val = parent.formField[this.codeField].split(",");
      }
      // return val;
      this.dynamicTags=val
      // for (let i = 0; i < this.interestList.length; i++) {
      //   for (let j = 0; j < val.length; j++) {
      //     if (this.interestList[i].lookupValueName == val[j]) {
      //       this.interestList[i].checked = true;
      //     }
      //   }
      // }
    }
  }
};
</script>
